package com.stylefeng.guns.modular.market.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.model.Expenditure;
import com.stylefeng.guns.modular.system.model.Market;
import com.stylefeng.guns.modular.system.dao.MarketMapper;
import com.stylefeng.guns.modular.market.service.IMarketService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.xmlbeans.impl.jam.mutable.MPackage;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.stereotype.Service;

import java.security.GeneralSecurityException;
import java.sql.Wrapper;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng123
 * @since 2018-07-27
 */
@Service
public class MarketServiceImpl extends ServiceImpl<MarketMapper, Market> implements IMarketService {

    /**
     * 描述:下载
     * 创建人: laoyu
     * 日期: 2018/8/6 0006
     * 时间: 下午 1:44
     */
    @Override
    public Map<String, Object> load(List<Market> marketList,String beginDate,String endDate,String platform)throws GunsException {

        Map<String,Object> resultMap = new HashMap<>();
        List<Map<String,Object>> marketMapList = new ArrayList<>();
        //组装下载数据
        double incomeTotal = 0;//收入总和
        double wcTotal = 0;//微信排名总支出
        double groupTotal = 0;//群排名总支出
        double qqTotal = 0;//qq推广总支出
        double otherTotal = 0;//其他支出总和
        double total = 0;//总支出
        for (Market market : marketList){
            Map<String,Object> loadMap = new HashMap<>();
            loadMap.put("revDate",market.getRevdate());
            loadMap.put("income",market.getIncome());
            loadMap.put("wcDraw",market.getWcDraw());
            loadMap.put("groupRanking",market.getGroupRanking());
            loadMap.put("qq",market.getQq());
            loadMap.put("otherPromotion",market.getOtherPromotion());
            loadMap.put("payoutTatol",market.getPayoutTatol());
            loadMap.put("balance",market.getBalance());
            loadMap.put("remarks",market.getRemarks());
            marketMapList.add(loadMap);
            incomeTotal += market.getIncome();
            wcTotal +=market.getWcDraw();
            groupTotal += market.getGroupRanking();
            qqTotal +=market.getQq();
            otherTotal +=market.getOtherPromotion();
            total += market.getPayoutTatol();
        }

        resultMap.put("marketMapList",marketMapList);
        resultMap.put("name",platform.toUpperCase()+beginDate+"至"+endDate+"RMB市场专用收入/支出明细分类表");
        resultMap.put("incomeTotal",incomeTotal);
        resultMap.put("wcTotal",wcTotal);
        resultMap.put("groupTotal",groupTotal);
        resultMap.put("qqTotal",qqTotal);
        resultMap.put("otherTotal",otherTotal);
        resultMap.put("total",total);

        return resultMap;
    }

    /**
     * 描述:统计市场收支明细
     * 创建人: laoyu
     * 日期: 2018/8/9 0009
     * 时间: 上午 11:39
     */
    @Override
    public Map<String, Object> total(EntityWrapper<Market> wrapper) throws GunsException {

        List<Market> marketList = this.selectList(wrapper);

        //组装下载数据
        double incomeTotal = 0;//收入总和
        double wcTotal = 0;//微信排名总支出
        double groupTotal = 0;//群排名总支出
        double qqTotal = 0;//qq推广总支出
        double otherTotal = 0;//其他支出总和
        for (Market market : marketList){
            if (market.getIncome()!=null) {
                incomeTotal += market.getIncome();
            }
            if (market.getWcDraw()!=null) {
                wcTotal += market.getWcDraw();
            }
            if (market.getGroupRanking()!=null) {
                groupTotal += market.getGroupRanking();
            }
            if (market.getQq()!=null) {
                qqTotal += market.getQq();
            }
            if (market.getOtherPromotion()!=null) {
                otherTotal += market.getOtherPromotion();
            }
        }
        Map<String,Object> totalMap = new HashMap<>();
        totalMap.put("incomeTotal",incomeTotal);
        totalMap.put("wcTotal",wcTotal);
        totalMap.put("groupTotal",groupTotal);
        totalMap.put("qqTotal",qqTotal);
        totalMap.put("otherTotal",otherTotal);
        totalMap.put("payoutTotal",incomeTotal+wcTotal+groupTotal+qqTotal+otherTotal);//总支出
        return totalMap;
    }


    /**
     * 描述:查询最后一笔报表余额
     * 创建人: laoyu
     * 日期: 2018/8/9 0009
     * 时间: 下午 2:05
     */
    @Override
    public double searchLast(String platform) throws GunsException {
        Market market = baseMapper.searchLast(platform);
        if (market==null)
            return 0;
        return market.getBalance();
    }

    /**
     * 描述:修改
     * 创建人: laoyu
     * 日期: 2018/8/9 0009
     * 时间: 下午 4:30
     */
    @Override
    public void update(Market oldMarket, Market market) throws GunsException {

        //计算余额差
        double incomePoor = market.getIncome()-oldMarket.getIncome();
        double wcPoor = market.getWcDraw()-oldMarket.getWcDraw();
        double gPoor = market.getGroupRanking()-oldMarket.getGroupRanking();
        double qqPoor = market.getQq()-oldMarket.getQq();
        double otherPoor = market.getOtherPromotion()-oldMarket.getOtherPromotion();
        double difference = incomePoor-wcPoor-gPoor-qqPoor-otherPoor;
        baseMapper.updateById(market);
        //修改该订单之后的所有余额
        baseMapper.updateBalance(market.getId(),ShiroKit.getUser().getDeptcode(),difference);

    }

    /**
     * 描述:删除报表修改报表余额
     * 创建人: laoyu
     * 日期: 2018/8/9 0009
     * 时间: 下午 4:56
     */
    @Override
    public void updateBalance(int id,Market market) throws GunsException {
        double difference = 0;//修改差额
        if (market.getIncome()!=null&&market.getIncome()!=0)
            difference += -market.getIncome();
        if (market.getWcDraw()!=null&&market.getWcDraw()!=0)
            difference += market.getWcDraw();
        if (market.getGroupRanking()!=null&&market.getGroupRanking()!=0)
            difference += market.getGroupRanking();
        if (market.getQq()!=null&&market.getQq()!=0)
            difference += market.getQq();
        if (market.getOtherPromotion()!=null&&market.getOtherPromotion()!=0)
            difference += market.getOtherPromotion();
        baseMapper.updateBalance(id,ShiroKit.getUser().getDeptcode(),difference);
    }
}
